var apiHost = wx.getStorageSync("apiHost");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio : '1',          //单点框标识
    carrageList:'',       //运费模板列表
    carrageListName: [],  //运费模板列表名称
    carrageId: '',        //运费模板Id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var deliveryMethodFlag = options.deliveryMethodFlag;  //初始化发货方式
    var userId = wx.getStorageSync("userId");             //用户id

    that.getDeliveryList(userId);                         //获取运费模板
    that.setData({
      userId: userId
    });
    
    //判断发货方式参数
    if (deliveryMethodFlag != undefined) {
      //发货方式
      that.setData({
        radio: deliveryMethodFlag
      })
    }
  },

  /**
   * radio 选择方式 发货方式
   */
  radioChange: function (e) {
    var that = this;

    var flag = e.currentTarget.dataset.toggle;
    wx.setStorageSync('deliveryMethod', flag);

    that.setData({
      radio: flag
    });
    that.goSecondOrdinaryCollage(); //跳转第二次拼团
  },

  /**
   * 获取配送列表
   */
  getDeliveryList: function (userId){
    var that = this;

    wx.showLoading({
      title: '加载中',
      icon: "loading",
      mask: true
    });

    wx.request({
      url: apiHost + 'hpp/carrage/selectList',
      method:'get',
      header:{
        "content-type":"application/json"
      },
      data:{
        userId:userId,
      },
      success : function(res){
        var code = res.data.code;
        var msg = res.data.msg;

        if("10000" == code){
          var carrageList = res.data.data;
          var carrageListName = that.data.carrageListName;
          
          for (var i = 0; i < carrageList.length;i++){
            carrageListName.push(carrageList[i].name)
          }
          that.setData({
            carrageList: carrageList,
            carrageListName: carrageListName
          });
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 5000,
            mask: true
          });
        }
        wx.hideLoading();
      },
      fail :function(){
        wx.hideLoading();   //隐藏加载loading
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 5000,
          mask: true
        });
      }
    })
  },

  /**
   * pick 选择器
   */
  bindPickerChange: function (e) {
    var that = this;
    var index = e.detail.value;

    var carrageList = that.data.carrageList;
    var carrageId = carrageList[index].carrageTemplateId;
    wx.setStorageSync('carrageId', carrageId);

    wx.setStorageSync('deliveryMethod', 'delivery');
    that.setData({
      carrageId: carrageId,
      radio: 'delivery'
    })
    that.goSecondOrdinaryCollage(); //跳转第二次拼团
  },
  /**
   * 跳转 第二步发布拼团
   */
  goSecondOrdinaryCollage:function(){
    var that = this;
    var flag = that.data.radio;

    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });
    //点击返回第二次发布拼团   (携带发货方式)
    wx.reLaunch({
      url: '../secondOrdinaryCollage/secondOrdinaryCollage?deliveryMethod=' + flag,
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})